package com.evan.webadmin.biz.controller;

import com.evan.common.core.annotation.NoAuthInterface;
import com.evan.common.core.object.ResponseResult;
import com.evan.common.core.upload.UploadResponseInfo;
import com.evan.common.minio.util.MinioUpDownloader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/admin/biz/file")
public class FileController {

    @Autowired
    MinioUpDownloader minioUpDownloader;

    @PostMapping("/uploadImg")
    public ResponseResult<UploadResponseInfo> uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws IOException {
        UploadResponseInfo responseInfo = minioUpDownloader.doUpload(req.getContextPath(), "", "","", true, file);
        return ResponseResult.success(responseInfo);
    }

    @GetMapping("/uploadImg/download/{fileName}")
    @NoAuthInterface
    public void download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        minioUpDownloader.doDownload("", "","", fileName,true, response);
    }
}
